package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.BankBean;
import com.koron.css2.baseConfig.mapper.BankMapper;
import com.koron.css2.clientCenter.bean.AccountInfoBean;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 账户信息详情
 * 
 * @author 甄玉晴 2020年4月7日
 */
public class AccountInfoQuery implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			// 入参转换
			AccountInfoBean bean = JsonUtils.objectToPojo(req.getData(), AccountInfoBean.class);
			if (StringUtils.isBlank(bean.getAccountNo())) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "账户编号不能为空。", Void.class);
			}
			AccountInfoMapper mapper = factory.getMapper(AccountInfoMapper.class);
			// 根据账户编号查询账户信息
			AccountInfoBean accountInfo= mapper.getAccountInfoByCode(bean.getAccountNo(), AccountInfoBean.ACCOUNT);
			if (accountInfo == null) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "账户编号不存在。", Void.class);
			}
			//获取数据字典信息
			if (accountInfo != null && StringUtils.isNotBlank(accountInfo.getAccountType())) {
				accountInfo.setAccountTypeName(Tools.getDicNameByCodeAndValue(factory, "ACT", accountInfo.getAccountType()));
			}
			if (accountInfo != null && StringUtils.isNotBlank(accountInfo.getPayWay())) {
				accountInfo.setPayWayName(Tools.getDicNameByCodeAndValue(factory, "BPW", accountInfo.getPayWay()));
			}
			if (accountInfo != null && StringUtils.isNotBlank(accountInfo.getInvoiceType())) {
				accountInfo.setInvoiceTypeName(Tools.getDicNameByCodeAndValue(factory, "BBT", accountInfo.getInvoiceType()));
			}
			if (accountInfo != null && StringUtils.isNotBlank(accountInfo.getDeliverWay())) {
				accountInfo.setDeliverWayName(Tools.getDicNameByCodeAndValue(factory, "SDW", accountInfo.getDeliverWay()));
			}
			BankMapper bankeMapper = factory.getMapper(BankMapper.class);
			//获取开户行名称
			if(!StringUtils.isBlank(accountInfo.getOpenAccountBank())) {
				BankBean bankName = bankeMapper.findBankById(accountInfo.getOpenAccountBank());
				accountInfo.setOpenAccountBankName(bankName==null?"":bankName.getBankName());
				
			}
			if(StringUtils.isNotBlank(accountInfo.getVatFlag()) && "1".equals(accountInfo.getVatFlag())) {
				accountInfo.setVatFlagName("是");
			}else {
				accountInfo.setVatFlagName("否");
			}
			//获取购买方开户行名称
			if(!StringUtils.isBlank(accountInfo.getInvoiceBank())) {
				BankBean invoiceName = bankeMapper.findBankById(accountInfo.getInvoiceBank());
				accountInfo.setInvoiceBankName(invoiceName==null?"":invoiceName.getBankName());
			}
			// 返回结果集
			MessageBean<AccountInfoBean> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "sucess", AccountInfoBean.class);
			msg.setData(accountInfo);
			return msg;
		} catch (Exception e) {
			logger.error("根据客户编号查询账户查询失败" + e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", Void.class);
		}
	}

}
